package com.niiwoo.civet.mobile.service.bill.detail;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.account.dto.common.BankCardDTO;
import com.niiwoo.civet.account.dto.common.WithholdOrderDTO;
import com.niiwoo.civet.account.dto.response.BankInfoRespDTO;
import com.niiwoo.civet.account.dto.response.NiiwooBillRespDTO;
import com.niiwoo.civet.account.dto.response.UserBillBaseResponseDTO;
import com.niiwoo.civet.account.enums.OrderTypeEnum;
import com.niiwoo.civet.account.service.bankcard.BankCardDubboService;
import com.niiwoo.civet.account.service.recharge.RechargeDubboService;
import com.niiwoo.civet.account.service.trade.WithholdDubboService;
import com.niiwoo.civet.mobile.vo.response.bill.UserBillDetailResponse;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Strings;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 充值账单详情
 * Created by xiaoyingping on 2018/11/7.
 */
@Service
public class RechargeBillDetailStrategyImpl implements BillDetailStrategy {

    @Reference(version = "1.0.0")
    private RechargeDubboService rechargeDubboService;
    @Reference(version = "1.0.0")
    private WithholdDubboService withholdDubboService;
    @Reference(version = "1.0.0")
    private BankCardDubboService bankCardDubboService;

    @Override
    public String build(UserBillBaseResponseDTO userBillBase, List<NiiwooBillRespDTO> userBillFlowList, UserBillDetailResponse response) {
        // 查询银行名称+银行卡号后4位
        String content = "";
        if(OrderTypeEnum.RECHARGE_AUTO.getCode().equals(userBillBase.getOrderType())){
            //代扣充值
            WithholdOrderDTO withholdOrderDTO = withholdDubboService.queryWithholdOrderByApplyOrderId(Long.valueOf(userBillBase.getOrderId()));
            BankCardDTO bankCardDTO = bankCardDubboService.getBankCardInfoById(withholdOrderDTO.getBankCardId());
            String bankName = bankCardDTO.getBankType().getBankName();
            String bankNo = bankCardDTO.getBankAccountNo();
            String subBankNo = StringUtils.right(bankNo, 4);;
            content = bankName + "(" + subBankNo + ")";

        }else {
            //用户充值
            BankInfoRespDTO bankInfoRespDTO = rechargeDubboService.getBankInfoByOrderId(Long.valueOf(response.getOrderId()));
            if(Objects.nonNull(bankInfoRespDTO)){
                String bankName = bankInfoRespDTO.getBankName();
                String bankNo = bankInfoRespDTO.getBankNo();
                String subBankNo = StringUtils.right(bankNo, 4);
                content = bankName + "(" + subBankNo + ")";
            }
        }

        if(!Strings.isNullOrEmpty(content)){
            UserBillDetailResponse.SpecialItem specialItem = response.new SpecialItem();
            specialItem.setName("充值渠道");
            specialItem.setContent(content);
            response.getSpecialItems().add(specialItem);
        }

        return JSON.toJSONString(response);
    }
}
